package com.snake.mylibrary.runtime;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

public final class RuntimeTools {
	private static Context mContext;
	private static UiHandler uiHandler;
	private static boolean mSystemApp;
	private static boolean mInstallPermission;
	
	public static void Init(Context ctx)
	{
		mContext = ctx;
		uiHandler = new UiHandler(mContext.getMainLooper());
		mSystemApp = ((ctx.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) > 0);
		try {
			ctx.enforceCallingOrSelfPermission(
					android.Manifest.permission.INSTALL_PACKAGES, null);
			mInstallPermission = true;
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}
	
	public static void RunOnUiThread(Context ctx, Runnable run)
	{
		uiHandler.post(run);
	}
	
	public static void RunOnUiThread(Context ctx, Runnable run, long delay)
	{
		uiHandler.postDelayed(run, delay);
	}
	

	/**
     * 检查代码是否运行在主线程
     * @param ctx
     * @return
     */
    public static boolean RunInMainThread(Context ctx)
    {
    	return Thread.currentThread() == ctx.getMainLooper().getThread();
    }
    
    /**
     * 系统中是否包含SD卡
     * @return
     */
    public static boolean SDCardExisit()
    {
    	return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
    
    /**
     * SD卡剩余空间
     * @return
     */
    public static long getSDCardFreeSpace()
    {
    	if(!SDCardExisit())
    	{
    		return 0L;
    	}
		File path = Environment.getExternalStorageDirectory();
		return getPathFreeSize(path.getAbsolutePath());  
    }
    
    /**
     * 系统剩余空间
     * @return
     */
    public static long getSystemFreeSpace()
    {
    	File data = Environment.getDataDirectory().getAbsoluteFile();
    	return getPathFreeSize(data.getAbsolutePath());
    }
    
    private static long getPathFreeSize(String f)
    {
    	StatFs sf = new StatFs(f);
    	long blockSize = sf.getBlockSize();
    	long freeBlock = sf.getAvailableBlocks();
    	return freeBlock * blockSize;
    }
    
    
    /**
     * 是否Wifi网络
     * @param ctx
     * @return
     */
    public static boolean isWifiNetwork(Context ctx) {
		ConnectivityManager cmgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cmgr.getActiveNetworkInfo();
		if (ni == null) {
			return false;
		}
		return ni.getType() == ConnectivityManager.TYPE_WIFI
				|| ni.getType() == ConnectivityManager.TYPE_WIMAX;
	}
	
    /**
     * 是否移动网络
     * @param ctx
     * @return
     */
	public static boolean isMobileNetwork(Context ctx) {
		ConnectivityManager cmgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cmgr.getActiveNetworkInfo();
		if (ni == null) {
			return false;
		}
		return !(ni.getType() == ConnectivityManager.TYPE_WIFI ||
				ni.getType() == ConnectivityManager.TYPE_WIMAX) ;
	}
	
	/**
	 * 当前是否有可用网络
	 * @param ctx
	 * @return
	 */
	public static boolean isNetworkAvailable(Context ctx) {
		ConnectivityManager cmgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cmgr.getActiveNetworkInfo();
		return ni != null;
	}
	
	public static boolean isSystemApp(Context ctx)
	{
		return mSystemApp;
	}
	
	public static boolean canInstallBackground(Context ctx)
	{
		return mInstallPermission && mSystemApp;
	}
}
